import copy
from collections import OrderedDict

from goods.models import GoodsChannel, SKU, SKUImage, SKUSpecification, SPUSpecification, SpecificationOption


def get_breadcrumb(cat3):
    """
    面包屑导航数据获取
    查询二级分类和一级分类
    """

    cat2 = cat3.parent
    cat1 = cat2.parent

    breadcrumb = {
        'cat1': cat1.name,
        'cat2': cat2.name,
        'cat3': cat3.name
    }

    return breadcrumb


def get_categories():
    """商品分类数据获取"""
    categories = OrderedDict()
    channels = GoodsChannel.objects.order_by('group_id', 'sequence')

    for channel in channels:
        group_id = channel.group_id

        if group_id not in categories:
            categories[group_id] = {
                'channels': [],
                'sub_cats': []
            }
        # 获取频道对应的一级分类
        cat1 = channel.category

        categories[group_id]['channels'].append({
            'id': cat1.id,
            'name': cat1.name,
            'url': channel.url
        })

        cat2s = cat1.subs.all()

        for cat2 in cat2s:
            cat2.sub_cats = []

            # 获取三级分类
            cat3s = cat2.subs.all()
            for cat3 in cat2.subs.all():
                cat2.sub_cats.append(cat3)
            categories[group_id]['sub_cats'].append(cat2)

    return categories


def get_goods_and_spec(sku_id):
    """SKU商品详情数据获取"""
    # 获取指定sku商品数据
    sku = SKU.objects.get(id=sku_id)

    # 获取SKU商品图片数据
    sku.images = SKUImage.objects.filter(sku=sku)

    keys = []
    # [13, 20]
    sku_specs = SKUSpecification.objects.filter(sku=sku).order_by('spec_id')

    for sku_spec in sku_specs:
        keys.append(sku_spec.option_id)

    spu = sku.spu

    sku_skus = SKU.objects.filter(spu=spu, is_launched=True)
    skus_dict = {}

    for temp_sku in sku_skus:
        sku_specs = SKUSpecification.objects.filter(sku=temp_sku).order_by('spec_id')

        temp_keys = []
        for sku_spec in sku_specs:
            temp_keys.append(sku_spec.option_id)

        skus_dict[tuple(temp_keys)] = temp_sku.id

    # 在其他规格选项不变的情况下，遍历在每个选项上绑定对应的 SKU 商品的ID
    spu_specs = SPUSpecification.objects.filter(spu=spu).order_by('id')

    for index, spu_spec in enumerate(spu_specs):
        # 获取该规格下的所有选项数据
        spec_options = SpecificationOption.objects.filter(spec=spu_spec)

        temp_keys = copy.deepcopy(keys)

        for spec_option in spec_options:
            temp_keys[index] = spec_option.id
            spec_option.sku_id = skus_dict.get(tuple(temp_keys))

        spu_spec.spec_options = spec_options

    return spu, spu_specs, sku


